package com.fashion.web.actions.management.person;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.fashion.model.person.Person;
import com.fashion.service.person.PersonService;
import com.fashion.utils.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@ParentPackage("json-default")
@Namespace("/management/person")
public class PersonAdminAction extends ActionSupport {
	public static Logger LOGGER = Logger.getLogger(PersonAdminAction.class);
	
	Map result = new HashMap();
	private String firstChar = "a";
	private String name;
	private String findPattern = "FIRST_CHAR";
	private Person personForm = new Person();
	private File uploadify;
	private String uploadifyFileName;
	@Resource(name = "personService")
	private PersonService personService;
	private int rows;
	private int page;

	@Action(value = "find_persons", results = { @Result(name = SUCCESS, type = "json", params = {
			"root", "result" }) })
	public String find() throws Exception {
		if("FIRST_CHAR".equals(findPattern)){
			long total = 0;
			List<Person> persons;
			if("others".equals(firstChar)){
				total = personService.countNotStartWithEn();
				persons = personService.findNotStartWithEn(rows
						* (page - 1), rows);
				
			}else{
				total = personService.countByFirstChar(firstChar);
				persons = personService.findByFirstChar(firstChar, rows
						* (page - 1), rows);			
			}
			result.put("total", total);
			result.put("rows", persons);
		}else{
			long total = 0;
			List<Person> persons;
			total = personService.countByName(name);
			persons = personService.findByName(name, rows
					* (page - 1), rows);
			result.put("total", total);
			result.put("rows", persons);
		}
		return SUCCESS;
	}

	@Action(value = "person_index", results = { @Result(name = SUCCESS, location = "person.jsp") })
	public String index() throws Exception {
		return SUCCESS;
	}
	
	@Action(value = "delete_person", results = { @Result(name = SUCCESS, location = "person.jsp") })
	public String deletePerson() throws Exception {
		personService.delete(personForm);
		return SUCCESS;
	}

	@Action(value = "add_person", results = { @Result(name = SUCCESS, location = "person_form.jsp") })
	public String addPerson() throws Exception {
		return SUCCESS;
	}

	@Action(value = "update_person", results = { @Result(name = SUCCESS, location = "person_form.jsp") })
	public String updatePerson() throws Exception {
		personForm = personService.findById(personForm.getId());
		return SUCCESS;
	}

	@Action(value = "add_or_update_person", results = { @Result(name = SUCCESS, location = "person.jsp") })
	public String addUpdatePerson() throws Exception {
		if (personForm.getId() == null || personForm.getId().equals("")) {
			personService.add(personForm);
		} else {
			personService.update(personForm);
		}
		return SUCCESS;
	}

	@Action(value = "add_logo", results = { @Result(name = SUCCESS, type = "json", params = {
			"root", "personForm.logo" }) })
	public String addLogo() {
		LOGGER.debug("add_logo");
		String destPath = "";
		String folder = ServletActionContext.getServletContext()
				.getRealPath("/person/");
		if (personForm.getId() != null && !personForm.getId().equals("")) {
			personForm = personService.findById(personForm.getId());
			String oldLogo = personForm.getLogo();
			if (oldLogo != null && !"".equals(oldLogo)) {
				// delete old one
				String oldPath = folder + File.separator + personForm.getLogo();
				File old = new File(oldPath);
				old.delete();
				destPath = oldPath;
			}
		}
		
		if("".equals(destPath)){
			String extName = "";
			if (uploadifyFileName.lastIndexOf(".") >= 0) {
				extName = uploadifyFileName.substring(uploadifyFileName.lastIndexOf("."));
			}
			String fileName = StringUtils.createTimeStamp() + extName;
			destPath = folder + File.separator + fileName;
			personForm.setLogo(fileName);
		}
		File dest = new File(destPath);
		LOGGER.debug("New logo will be moved to " + destPath);
		try {
			FileUtils.copyFile(uploadify, dest);
		} catch (IOException e) {
			LOGGER.error(e);
		}
		LOGGER.debug("New logo has be moved to " + destPath);
		return SUCCESS;
	}

	@Action(value = "columnForm", results = { @Result(name = SUCCESS, location = "../../form_result.jsp") })
	public String add() {
		personService.add(personForm);
		return SUCCESS;
	}

	public Map getResult() {
		return result;
	}

	public void setFirstChar(String firstChar) {
		this.firstChar = firstChar;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public Person getPersonForm() {
		return personForm;
	}

	public void setPersonForm(Person personForm) {
		this.personForm = personForm;
	}

	public String getFirstChar() {
		return firstChar;
	}

	public void setUploadify(File uploadify) {
		this.uploadify = uploadify;
	}

	public void setUploadifyFileName(String uploadifyFileName) {
		this.uploadifyFileName = uploadifyFileName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFindPattern() {
		return findPattern;
	}

	public void setFindPattern(String findPattern) {
		this.findPattern = findPattern;
	}
	
}